//
//  SYRouterManager.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2018/11/19.
//  Copyright © 2018年 Lssyyy. All rights reserved.
//

#import "SYRouterManager.h"
#import <MGJRouter/MGJRouter.h>
#import "SignInVC.h"
#import "SYRootNaviController.h"
#import "UIViewController+Hierarchy.h"
static NSString *const Router_PushToSignInViewControllerURL = @"SYRouter://pushToSignInViewController";
#define TopestViewController \
([UIViewController topViewController])
@implementation SYRouterManager
+ (instancetype)sharedRouteManager{
    static id _shareInstance = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        _shareInstance = [[self alloc]init];
    });
    return _shareInstance;
}

- (instancetype)init{
    if (self = [super init]) {
        [self registerURLPattern];
    }
    return self;
}

- (void)registerURLPattern {
    [self registerSignInURLPattern];
}

- (void)registerSignInURLPattern{
    [MGJRouter registerURLPattern:Router_PushToSignInViewControllerURL toHandler:^(NSDictionary *routerParameters) {
        NSDictionary *userInfo = routerParameters[MGJRouterParameterUserInfo];
        NSLog(@"字典内容%@",userInfo);
        void (^block)(id object) = routerParameters[MGJRouterParameterCompletion];
        BOOL isLogin = NO;
        if(isLogin){
            block(nil);
            return ;
        }else {
            SignInVC *loginController = [[SignInVC alloc] init];
            loginController.signInCompletionBlock = routerParameters[MGJRouterParameterCompletion];
            SYRootNaviController *navigationController = [[SYRootNaviController alloc] initWithRootViewController:loginController];
            [TopestViewController presentViewController:navigationController
                                               animated:YES
                                             completion:nil];
        }
    }];
}
- (void)openSignInURLWithUserInfo:(NSDictionary *)userInfo completion:(void (^)(id result))completion {
    
    [MGJRouter openURL:Router_PushToSignInViewControllerURL withUserInfo:userInfo completion:completion];
}
@end
